package vip.xiaonuo.smzq.modular.deom;

import com.healthmarketscience.jackcess.Column;
import com.healthmarketscience.jackcess.Database;
import com.healthmarketscience.jackcess.DatabaseBuilder;
import com.healthmarketscience.jackcess.Table;

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 读取mdb文件工具类
 */

public class MdbTool {

    /**
     * 读取mdb文件
     * @param filePath mdb文件路径
     * @param tableName table名称
     *
     *
     * @return
     * @throws Exception
     */
    public static List<Map<String, Object>> readMdbFile(String filePath, String tableName) throws Exception {
        File file = new File(filePath);
        List<Map<String, Object>> results = new ArrayList<>();

        try (Database db = new DatabaseBuilder().open(file)) {
            db.getTableNames();

            Table table = db.getTable(tableName);
            processTable(table, results);
        }

        return results;
    }

    private static void processTable(Table table, List<Map<String, Object>> results) {
        try {
            table.forEach(row -> {
                Map<String, Object> record = new HashMap<>();
                for (Column column : table.getColumns()) {
                    record.put(column.getName(), row.get(column.getName()));
                }
                results.add(record);
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }








}
